package com.sltx.util;

import com.quantum.auth.RSA2048;
import com.sltx.util.http.ConfigUtils;
import com.sltx.vo.PostJsonParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 转换Class
 * @author lizhiqiang
 *
 */
public class Conversion implements Runnable {
	
	
    public static String CONIG_FILE_NAME = "config.properties";
    public static String THEA_KEY_FILE_NAME = "thea-key.properties";
    public static String CONIG_KEY_UID = CommonUtils.getProperty(CONIG_FILE_NAME, "key.uid");//龙信
    public static String CONIG_KEY_NAME =  "key.filename";  
    public static String CONIG_DATACENTER_URL = "datacenter.url";
    public static String keyPath = CommonUtils.getProperty(THEA_KEY_FILE_NAME , SystemUtils.getSystemType());
	
	/**
	 * URL decode UTF-8
	 * @param str
	 * @return
	 */
	public static String codeConversion(String str){
		String result = "";
		try {
			result = URLDecoder.decode(str,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	 /**
     * Get key by path (Via configuration file)
     * 获取项目文件
     * @param keyPath
     * @return
     * @throws Exception
     */
    public static Key getConfigurationKey(String KeyName) throws Exception {
        Key key = null;
        try {
        	System.out.println(ConfigUtils.getProperty(KeyName, ""));
        	//获取公钥
        	//URL url = Conversion.class.getResource("../../key/" +ConfigUtils.getProperty(KeyName, ""));
			URL url = Conversion.class.getClassLoader().getResource(ConfigUtils.getProperty(KeyName, ""));
	   		key = RSA2048.getKey(url.getPath());//密钥路径
        } catch (Exception ex) {
            throw ex;
        }
        return key;
    }
    
    /**
     * Get key by path (Via local file)
     * 获取本地文件
     * @param keyPath
     * @return
     * @throws Exception
     */
    public static Key getLocalKey(String keyPath) {
        Key key = null;
    	 try {
    		//获取公钥
			key = RSA2048.getKey(keyPath);// 密钥路径
		} catch (Exception e) {
			e.printStackTrace();
		}
        return key;
    }
    /**
     * get service data
     * 获取 对应接口的数据
     * @param service 接口名称
     * @param params 接口对应参数
     * @return*/
     
    public static JSONObject getServiceData(String service,PageData params){
    	Key key = null;
		try {
			key = Conversion.getLocalKey(Conversion.keyPath + "public_" + Conversion.CONIG_KEY_UID + ".key");
//			key = Conversion.getConfigurationKey(Conversion.CONIG_KEY_NAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	 PostJsonParam jsonParam = new PostJsonParam();
         jsonParam.setUid(Conversion.CONIG_KEY_UID);
         jsonParam.setService(service);
         jsonParam.setParams(params);
         HttpUtils httpUtils = new HttpUtils(key);
         String result = httpUtils.sendRequest(jsonParam);
         System.out.println("龙信数据接口SERVICE:["+service+"\t ]RESULT："+result+"\t\n");
         return JSONObject.fromObject(result);
    }
    
    /**
     * @描述：返回参数为加入 jsonp 回调函数
     * @param
     * @param
     * @return String
     * @author lizhiqiang
     */
    
    public static String convertJsonString(List<Map<String,Object>> list,Map<String,Object> map,Object obj){
    	String jsonpResult = null;
    	if(map==null && list ==null){
    		System.out.println("=========返回数据为空=========");
    	}else if(map !=null && list !=null && map.size()>0 && list.size()>0){
    		System.out.println("=========目前只支持一种类型LIST or MAP=========");
    	}else if (map !=null && map.size()>0){
    		JSONObject json = JSONObject.fromObject(map);
    		jsonpResult= obj+"("+json+")";
    	}else if(list !=null && list.size()>0){
    		JSONArray json = JSONArray.fromObject(list);
    		jsonpResult= obj+"("+json+")";
    	}
    	if(jsonpResult==null){
    		map = new HashMap<String,Object>();
    		map.put("res", "数据为空");
    		JSONObject json = JSONObject.fromObject(map);
    		jsonpResult= obj+"("+json+")";
    	}
    	return jsonpResult;
    }
    
    public static List<Map<String,Object>> getInterfaceData(String service,PageData params){
    	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Key key = null;
		try {
//			key = Conversion.getLocalKey(Conversion.keyPath + "public_" + Conversion.CONIG_KEY_UID + ".key");
			key = Conversion.getConfigurationKey(Conversion.CONIG_KEY_NAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	PostJsonParam jsonParam = new PostJsonParam();
        jsonParam.setUid(Conversion.CONIG_KEY_UID);
        jsonParam.setService(service);
        jsonParam.setParams(params);
        HttpUtils httpUtils = new HttpUtils(key);
        String result = httpUtils.sendRequest(jsonParam);
        System.out.println("龙信数据接口RESULT："+result+"/n/t");
        JSONObject obj = JSONObject.fromObject(result);
        list = obj.getJSONArray("RESULTDATA");
        System.out.println("龙信数据接口RESULT："+result+"\n\t");
        return list;
    }


	@Override
	public void run() {
		
	}
}
